package com.liao.starapp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.liao.starapp.luck.Luck;
import com.liao.starapp.user.AboutActivity;
import com.liao.starapp.user.FeedbackActivity;
import com.liao.starapp.user.InfoActivity;
import com.liao.starapp.user.ItemUserGridViewLayoutAdapter;
import com.liao.starapp.user.UpdateLogActivity;
import com.liao.starapp.utils.GetAssetsData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class FragmentUserLayoutFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {


    private ImageView head_img;
    private LinearLayout about_ll;
    private LinearLayout info_ll;
    private LinearLayout updateLog_ll;
    private LinearLayout feedback_ll;
    private TextView tv_name;

    ArrayList<Luck> lucks;

    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        context = getContext();
        head_img = view.findViewById(R.id.head_img);
        about_ll = view.findViewById(R.id.about_ll);
        updateLog_ll = view.findViewById(R.id.updateLog_ll);
        feedback_ll = view.findViewById(R.id.feedback_ll);
        tv_name = view.findViewById(R.id.tv_name);
        info_ll = view.findViewById(R.id.info_ll);

        head_img.setOnClickListener(this);
        about_ll.setOnClickListener(this);
        updateLog_ll.setOnClickListener(this);
        feedback_ll.setOnClickListener(this);
        info_ll.setOnClickListener(this);

        lucks = new ArrayList<>();
    }

    private AlertDialog dialog;

    private void alertSelectImgDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View inflate = getLayoutInflater().inflate(R.layout.alert_user_layout, null);
        builder.setView(inflate);
        dialog = builder.show();
        GridView gridView = inflate.findViewById(R.id.gridView);
        String text = GetAssetsData.getGridViewText(context, "yunshis/yunshi.json");
        json(text);
        ItemUserGridViewLayoutAdapter adapter = new ItemUserGridViewLayoutAdapter(context, lucks);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);
    }

    private void json(String s) {
        try {
            lucks = new ArrayList<>();
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String logoname = jsonObject.getString("logoname");
                String name = jsonObject.getString("name");
                Luck luck = new Luck();
                luck.setLogoname(logoname);
                luck.setName(name);
                lucks.add(luck);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.head_img:
                alertSelectImgDialog();
                break;
            case R.id.about_ll:
                startActivity(new Intent(context, AboutActivity.class));
                break;
            case R.id.info_ll:
                startActivity(new Intent(context, InfoActivity.class));
                break;
            case R.id.updateLog_ll:
                startActivity(new Intent(context, UpdateLogActivity.class));
                break;
            case R.id.feedback_ll:
                startActivity(new Intent(context, FeedbackActivity.class));
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Luck luck = lucks.get(position);
        Bitmap bitmap = GetAssetsData.getGridViewImg(context, "xzcontentlogo/" + luck.getLogoname() + ".png");
        head_img.setImageBitmap(bitmap);
        tv_name.setText(luck.getName());
        dialog.dismiss();
    }
}
